import {Inject, Injectable} from '@nestjs/common';
import {IMongoFind} from "../util/database.query.interface";
import {MongoRepository} from 'typeorm';
import {Category} from "./databases/mongo/book/category.entity";
import {Books} from "./databases/mongo/book/books.entity";

@Injectable()
export class BookManagerService {
    constructor(
        @Inject('categoryModelToken')
        private readonly categoryModel: MongoRepository<Category>,
        @Inject('booksModelToken')
        private readonly booksModel: MongoRepository<Books>,
    ) {
    }

    async getCategoryNameList(param?: IMongoFind): Promise<Category[]> {
        let res = this.categoryModel.find();
        // if (param.sort) {
        //     res.sort(param.sort);
        // }
        return await res;
    }

    async refreshCategory() {
        // let old_name_count = {};
        // (await this.getCategoryNameList()).filter(row => row.name).map((row: Category) => {
        //     old_name_count[row.name] = row.order || 0;
        // });
        //
        // let new_category_rows = (await this.booksModel.aggregate([
        //     {$match: {}},
        //     {$group: {_id: "$majorCate"}}
        // ]).exec()).filter(row => row._id).map((row: Books) => {
        //     return {
        //         name: row._id,
        //         count: 0,
        //     };
        // });

        // new_category_rows.map(function (row: Category) {
        //     if (old_name_count.hasOwnProperty(row.name)) {
        //         row.order = old_name_count[row.name];
        //     }
        //     return row;
        // });
        //
        // if (new_category_rows) {
        //     await this.categoryModel.delete({});
        //     await this.categoryModel.insertMany(
        //         new_category_rows
        //     );
        //
        // }
        return true;
    }

//     updateCategory(createCatDto: CreateCatDto1) {
//         console.log(createCatDto)
//         // return await this.categoryModel.update(
//         //     {}
//         // );
//
//     return null;
// }
}